package projectServlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.CompensationDAO;
import dao.CompensationDAOFactory;
import dao.ConnPool;
import dao.DAOException;
import dao.EmployeeDAO;
import dao.EmployeeDAOFactory;
import dao.mysql.CompensationDAOFactoryImpl;
import dao.mysql.EmployeeDAOFactoryImpl;
import dto.CompensationDTO;
import dto.UserDTO;

/**
 * Servlet implementation class Compensationservlet
 */
public class Compensationservlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	HttpSession sess =null;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Compensationservlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		doprocess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		doprocess(request, response);
	}

	private void doprocess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		CompensationDAOFactory CFac = new CompensationDAOFactoryImpl();
		CompensationDAO compDAO = CFac.getCompensationDAO();
		EmployeeDAOFactory fac = new EmployeeDAOFactoryImpl();
		EmployeeDAO empDAO = fac.getEmployeeDAO();
		UserDTO user= new UserDTO();
		
		String path = request.getPathInfo();
		//System.out.println("PATH IS : " + path);
		//System.out.println(path.equals("/Load"));
		//System.out.println(path.equals("/post"));
		if (path.equals("/Load"))
		{

			try
			{
				ConnPool.setupConPool(); // Test code due to no connection pool setup. 
			}
			catch (DAOException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//int userID = Integer.parseInt(request.getParameter("userID"));
			String SUserID = (String) request.getSession().getAttribute("id");
			int    UserID  = Integer.parseInt(SUserID);
			
			try
			{
				user = empDAO.findUser(UserID);
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			sess = request.getSession();
			
			sess.setAttribute("user", user);
			
			//request.setAttribute("user", user);

			RequestDispatcher rd = request
					.getRequestDispatcher("/ApplyCompensation.jsp");

			rd.forward(request, response);
		}

		if (path.equals("/post"))
		{
			int claims = Integer.parseInt(request.getParameter("claims"));
			//System.out.println(claims);
			
			UserDTO userUpdated = (UserDTO) sess.getAttribute("user");
			double left = userUpdated.get_overTimeDays()-claims;
			//System.out.println(userUpdated.get_name());
			userUpdated.set_overTimeDays(left);
			
			try
			{
				empDAO.updateUser(userUpdated);
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			CompensationDTO comp = new CompensationDTO();
			
			comp.set_approvingManager(userUpdated.get_supID());
			comp.set_numberOfDays(claims);
			comp.set_status("Pending");
			comp.set_userID(userUpdated.get_userID());
			comp.set_userName(userUpdated.get_name());
			
			try
			{
				compDAO.insertCompApplication(comp);
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
//			User account updated and new compensation application add. After this bring user to some where.
			RequestDispatcher rd = request
					.getRequestDispatcher("/viewdetail");

			rd.forward(request, response);
		}

	}

}
